﻿using David.CanBus;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using XaDevice;
namespace XaUserDemo
{
    internal class Program
    {
        List<NetworkCanDevice> DeviceInfos = null;
        XaDeviceClient XaClient;
        private void XaClient_NetworkCanDeviceHotPlugCallBack(List<NetworkCanDevice> deviceInfos)
        {
            DeviceInfos = deviceInfos;
            //Console.WriteLine("当前XA设备有:");
            foreach (NetworkCanDevice deviceInfo in deviceInfos)
            {
                Console.WriteLine($"{deviceInfo.DevicePath}");
            }

        }
        private Task XaClient_NetworkCanDeviceEventNotifyCallBack(string devicePath, uint evLen, DeviceEvent_t[] eve)
        {
            
            for (int i = 0; i < eve.Length; i++)
            {
                var e = eve[i];
                //Console.WriteLine($"{e.EventTag}");
                if (e.EventTag == DeviceEventTagType_t.CanXRxOk)
                {
                    var rx_frame = e.CanXRx;
                    //你的代码  最好是插入队列，不要再这里操作。
                }
                else if (e.EventTag == DeviceEventTagType_t.CanXTxReport)
                {
                    var tx_complete_frame = e.CanXTxReport;
                    //你的代码  不用就不管
                }
                else if (e.EventTag == DeviceEventTagType_t.CanXRxErrorEvent)
                {
                    var rx_error_frame = e.CanXRxErrorEvent;
                    //你的代码  不用就不管

                }
            }
            return Task.CompletedTask;
        }
        public async Task TestXaUserDemo()
        {
            //这部分放窗口的构造函数或者loading里
            XaClient = new XaDeviceClient();
            XaClient.NetworkCanDeviceEventNotifyCallBack += XaClient_NetworkCanDeviceEventNotifyCallBack;
            XaClient.NetworkCanDeviceHotPlugCallBack += XaClient_NetworkCanDeviceHotPlugCallBack;

            //这部分放到XA设备被选中时
            XaClient.Init();
            DeviceInfos =await XaClient.GetDevices();
            var dp = DeviceInfos[0].DevicePath;//以第一个设备为例
            await XaClient.Open(dp);

            //这部分放到配置波特率时
            var cfg = new CanDeviceConfig_t();
            cfg.IsEnableChannel = true;
            cfg.IsEnableFilter = false;
            cfg.BaudRateConfig = new BusRate_t() { Prescaler = 2, Ts1 = 63, Ts2 = 16, Sjw = 16, Rate = 500000 };
            cfg.FdDataRateConfig = new BusRate_t() { Prescaler = 2, Ts1 = 15, Ts2 = 4, Sjw = 4, Rate = 2000000 };
            cfg.FdMode = FdMode_t.ISO;
            cfg.FrameFormat = FrameFormat_t.FdBrs;
            cfg.RunMode = RunMode_t.Normal;
            await XaClient.Configure(dp, 0, cfg);

            //这部分放到配置完毕了，启动设备时
            await XaClient.SetCanDeviceRunState(dp, true);

            //这部分放到发送
            var p = new CANX_Packet_t()
            {
                ChannelIndex = 0,
                Data = new byte[8] { 1, 2, 3, 4, 5, 6, 7, 8 },
                DataLen = 8,
                Flags = CANX_Packet_Flags.Data | CANX_Packet_Flags.Std,
                Identifier = 1,
                Key = 0,
                TimeStamp = 0,
                UserData = 0,
            };
            var packets = new CANX_Packet_t[1];
            for (int i = 0; i < packets.Length; i++)
            {
                packets[i] = p;
            }
            List<TimeSpan> ts = new List<TimeSpan>();
            var sw = Stopwatch.StartNew();
            int testcnt = 0;
            while (true)
            {
                sw.Restart();
                XaClient.SendCanXFrame(dp, (uint)packets.Length, packets).Wait(500);
                sw.Stop();
                ts.Add(sw.Elapsed);
                testcnt++;
                if (testcnt > 100000)
                    break;
            }
            

            //用完关闭设备
            //await XaClient.Close(dp);
        }
        static void Main(string[] args)
        {
            var demo = new Program();
            var demotask = Task.Run(async () =>
            {
                await demo.TestXaUserDemo();
            });
            Thread.Sleep(-1); 
        }
    }

    
}
